/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.bluepumpkin.beans;

import com.aptech.bluepumpkin.Event;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Trong Nguyen
 */
@Stateless
public class EventFacade extends AbstractFacade<Event> {

    @PersistenceContext(unitName = "BluePumkin-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventFacade() {
        super(Event.class);
    }

    public List<Event> getAllActiveEvents() {
        String sql = "SELECT e FROM Event e WHERE  e.active = :active Order By e.startDate DESC";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        createQuery.setParameter("active", true);

        return createQuery.getResultList();
    }

    public List<Event> getAllActiveEventsWithPrizes() {
        String sql = "SELECT e FROM Event e WHERE  e.active = :active Order By e.startDate DESC";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        createQuery.setParameter("active", true);
        List<Event> resultList = createQuery.getResultList();
        List<Event> filter = new ArrayList<Event>();

        for (Event event : resultList) {
            if (null != event.getPrizeList() && event.getPrizeList().size() > 0) {
                filter.add(event);
            }
        }
        System.out.println("---getAllActiveEventsWithPrizes: "+filter.size());
        return filter;
    }

    /**
     * Get all Events USED BY ADMIN only
     *
     * @return List<Event>
     */
    public List<Event> getAllEvents() {
        String sql = "SELECT e FROM Event e Order By e.startDate DESC";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        return createQuery.getResultList();
    }

    public List<Event> getUpcomingEvents() {
        List<Event> list;
        Calendar cal = Calendar.getInstance();
        System.out.println("----TimeZone: " + cal.getTimeZone());
        cal.add(Calendar.DATE, -1);

        System.out.println("--Current date:" + cal.getTime().toString());
        String sql = "SELECT e FROM Event e WHERE e.startDate >= :startRegDate AND e.active = :active Order By e.startDate ASC";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        createQuery.setParameter("startRegDate", cal.getTime());
        createQuery.setParameter("active", true);

        list = createQuery.getResultList();
        if (list != null) {
            System.out.println("---found: " + list.size());
        } else {
            list = new ArrayList<Event>();
            System.out.println("---found: 0");
        }
        return list;
    }

    public List<Event> getCurrentEvents() {
        List<Event> list;
        Calendar cal = Calendar.getInstance();

        String sql = "SELECT e FROM Event e WHERE e.startDate <= :startRegDate AND e.endDate >= :endRegDate AND e.active = :active";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        createQuery.setParameter("startRegDate", cal.getTime());
        createQuery.setParameter("endRegDate", cal.getTime());
        createQuery.setParameter("active", true);
        list = createQuery.getResultList();
//        if(list!=null){
//            System.out.println("getCurrentEvents---found: "+list.size());
//        }else{
//            list = new ArrayList<Event>();
//             System.out.println("getCurrentEvents---found: 0");
//        }
        return list;
    }

    public List<Event> getPastEvents() {
        List<Event> list;
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        String sql = "SELECT e FROM Event e WHERE  e.endDate < :endRegDate AND e.active = :active";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);

        createQuery.setParameter("endRegDate", cal.getTime());
        createQuery.setParameter("active", true);
        list = createQuery.getResultList();
//        if(list!=null){
//            System.out.println("getCurrentEvents---found: "+list.size());
//        }else{
//            list = new ArrayList<Event>();
//             System.out.println("getCurrentEvents---found: 0");
//        }
        return list;
    }
}
